package edu.mbb.basepackage;

import java.io.File;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import edu.mbb.core.Configuration;

public class GetXMLConfiguration {

	public static void init(String xmlPath) {
		ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
		try {
			Enumeration<URL> resource = contextClassLoader.getResources(xmlPath);
			while (resource.hasMoreElements()) {
				URL nextElement = resource.nextElement();
				// 获取当前路径的名字
				String file = nextElement.getFile();
				File tem = new File(file);
				File[] list = tem.listFiles();
				for (File fileName : list) {

					if (fileName.exists()) {
						if (fileName.getName().endsWith(".xml")) {
							// 开始解析xml
							SAXReader sax = new SAXReader();
							Document read = sax.read(fileName);
							Element rootElement = read.getRootElement();
							// rootElement.get
							// System.out.println(name);
							// nameSpace 名字
							String namespace = rootElement.attributeValue("namespace");
							List<Element> elements = rootElement.elements();
							for (Element element : elements) {
								String id = element.attributeValue("id");
								String text = element.getText();
								Configuration.insertXmlToInFaceMethodSql(id, text);
							}
							Configuration.insertMapperInFaceToXml(namespace, Configuration.getXmlToInFaceMethodSql());
							System.out.println(Configuration.getmapperInFaceToXml());
						}
					}
				}

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		init("mapper");
	}
}
